//
//  SubscribeViewController.swift
//  Spookiness
//
//  Created by 腾億 on 2017/12/6.
//  Copyright © 2017年 tml. All rights reserved.
//

import UIKit

class SubscribeViewController: BaseViewController {

    fileprivate var subscribeListArr = [SubscribeComicListModel]()
    
    fileprivate lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 5
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.alwaysBounceVertical = true
        collectionView.register(RecommentComicHead.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "RecommentComicHead")
        collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footView")
        collectionView.register(SubscribeComicsCell.self, forCellWithReuseIdentifier: "SubscribeComicsCell")
        collectionView.refrshHead  = RefreshGifHeader{
            self.loadData()
        }
        collectionView.refreshFoot = RefreshBackFooter(with: "使用妖气币可以购买订阅漫画\nVIP会员购买还有优惠哦~")
        return collectionView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        
        loadData()
        view.addSubview(self.collectionView)
        setUpSnap()
        // Do any additional setup after loading the view.
    }

    fileprivate func setUpSnap() {
        self.collectionView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(self.view.snp.bottom).offset(-2*kTabbarHeight)
        }
    }
    
    fileprivate func loadData() {
        NetworkTool.loadHomeSubscribeData {[weak self] (subscribeListModel) in
            for item in subscribeListModel.newSubscribeList {
               self?.subscribeListArr.append(item)
            }
            self?.collectionView.refrshHead.endRefreshing()
            self?.collectionView.reloadData()
        }
    }
    
}

extension SubscribeViewController : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return subscribeListArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let comicList = subscribeListArr[section]
        return comicList.comics.count 
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SubscribeComicsCell", for: indexPath) as! SubscribeComicsCell
        let comicList = subscribeListArr[indexPath.section]
        cell.model = comicList.comics[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionElementKindSectionHeader {
            let head = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "RecommentComicHead", for: indexPath) as! RecommentComicHead
            let comicList = subscribeListArr[indexPath.section]
            head.iocnView.kf.setImage(urlString: comicList.titleIconUrl)
            head.titleLable.text = comicList.itemTitle
            return head
        }else {
            let foot = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footView", for: indexPath)
            foot.backgroundColor = kRGBA(r: 242, g: 242, b: 242, a: 1)
            return foot
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = floor(Double(kSCREEN_WIDTH - 10.0) / 3.0)
        return CGSize(width: width, height: 240)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return subscribeListArr.count  != section ? CGSize(width: kSCREEN_WIDTH, height: 10) : CGSize.zero
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        let comicList = subscribeListArr[section]
        return comicList.itemTitle?.count ?? 0 > 0 ? CGSize(width: kSCREEN_WIDTH, height: 44) : CGSize.zero
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.collectionView.deselectItem(at: indexPath, animated: true)
        let comicList = subscribeListArr[indexPath.section]
        guard let model: SubscribeComicsModel = comicList.comics[indexPath.row] else { return }
        let vc = ComicsDetailViewController(comicid: model.comicId)
        vc.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(vc, animated: true)
    }
    
}




